package com.code.question5.impl;

import com.alibaba.fastjson.JSON;
import com.code.question5.model.SendMessage;

import java.io.BufferedReader;
import java.io.IOException;
import java.net.SocketException;
import java.text.SimpleDateFormat;

/**
 * 异步接收消息线程类
 */
public class ReciveMessageServiceImpl implements Runnable {
    private BufferedReader br;
    private static SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    public volatile boolean exit = true;

    public ReciveMessageServiceImpl(BufferedReader br) {
        this.br = br;
    }

    @Override
    public void run() {
        String serverBack = null;
        while (exit) {
            try {
                serverBack = br.readLine();
            } catch (IOException e) {
                System.out.println("客户端已退出");
            }
            if (null != serverBack) {
                SendMessage sendMessage = JSON.parseObject(serverBack, SendMessage.class);
                System.out.println(sendMessage.getContent());
            }
        }
        try {
            br.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
